﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.Content
{
    [Serializable]
    public class PageTemplateConstraintCollection : ChildEntityCollection<PageTemplate, PageTemplateConstraint>
    {
        public PageTemplateConstraintCollection(PageTemplate parent, Func<PageTemplate, IEnumerable<PageTemplateConstraint>> loader)
            : base(parent, loader)
        {

        }

        public void Add(PageTemplate pageTemplate, int? minimumRequired, int? maximumAllowed, string nameConstraint)
        {
            if (pageTemplate == null)
                throw new ArgumentNullException("pageTemplate");

            if (minimumRequired.HasValue && minimumRequired < 0)
                throw new ArgumentException("MinimumRequired has to be greater than 0 if speficied");

            if (maximumAllowed.HasValue && maximumAllowed <= 0)
                throw new ArgumentException("MaximumAllowed has to be greater than 0 if specified");

            if(this.FirstOrDefault(pt => pt.RelatedTemplate.Equals(pageTemplate)) != null)
                throw new ArgumentException("Constraint already exists");

            base.Add(
                new PageTemplateConstraint()
                    {
                        RelatedTemplate = pageTemplate,
                        MaximumAllowed = maximumAllowed,
                        MinimumRequired = minimumRequired,
                        NameConstraint = nameConstraint
                    });
        }

        public new void Clear()
        {
            base.Clear();
        }

        public void Remove(PageTemplateConstraint constraint)
        {
            base.Remove(constraint);
        }
    }
}
